#!/usr/bin/env groovy
package com.yidao.www

def singleAutoBuild(project, branch){
    
    def singleBuildProfile = singleBuildProfile(project, branch)

    def label = singleBuildProfile.label
    def image = singleBuildProfile.image
    def gitUrl = singleBuildProfile.gitUrl.url
    def targetDir = singleBuildProfile.gitUrl.targetDir
    def buildScript = singleBuildProfile.buildScript
    def artifact = singleBuildProfile.artifact
    def artifactory = singleBuildProfile.artifactory
    def date = new Date()
    def base = new base()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println singleBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "gitUrl: ${gitUrl}"
    println "targetDir: ${targetDir}"
    println "artifact: ${artifact}"
    println "buildScript: ${buildScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins') {

            node(label){
                
                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    try{
                        base.gitCheckout(gitUrl, branch, targetDir)
                    }catch(e){
                        println '[ERROR] 代码克隆失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('compile'){
                    try{
                        def status = base.scriptStatus(buildScript)
                        if(status != 0){
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error '[ERROR] 编译失败!'
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('upload'){
                    print "artifact: ${artifact}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifact, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('influxDb'){
                    currentBuild.result = 'SUCCESS'
                    base.influxDb()
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }

        }

}

def commonAutoBuild(project, branch, version){

    def date = new Date()
    def commonBuildProfile = commonBuildProfile(project, branch, version)
    def label = commonBuildProfile.label
    def image = commonBuildProfile.image
    def gitUrl = commonBuildProfile.gitUrl
    def buildDir = commonBuildProfile.buildDir
    def buildScript = commonBuildProfile.buildScript
    def packageDir = commonBuildProfile.packageDir
    def packageScript = commonBuildProfile.packageScript
    def artifactName = commonBuildProfile.artifactName
    def artifactory = commonBuildProfile.artifactory

    def base = new base()
    def deployTarget = base.backupFolder(artifactory, project, branch, date)
    def server = Artifactory.server 'jfrog-artifactory'
    def srverUrl = server.url
    def storageURL

    println '============================================== BUILD INFO =============================================='
    println commonBuildProfile
    println "label: ${label}"
    println "image: ${image}"
    println "gitUrl: ${gitUrl}"
    println "artifactName: ${artifactName}"
    println "buildScript: ${buildScript}"
    println "packageScript: ${packageScript}"
    println "artifactory: ${artifactory}"
    println '========================================================================================================'

    podTemplate(
        cloud: 'kubernetes',
        containers: [
            containerTemplate(
                image: image,
                name: 'jnlp',
                ttyEnabled: true,
                alwaysPullImage: true,
                workingDir: '/home/jenkins/agent')
        ],
        label: label,
        name: 'yidao',
        namespace: 'kube-jenkins') {

            node(label){

                stage('checkout'){
                    storageURL = srverUrl + '/' + deployTarget
                    base.gitClone(gitUrl, branch)
                }

                stage('compile'){
                    try{
                        dir(buildDir){
                            def status = base.scriptStatus(buildScript)
                            if(status != 0){
                                currentBuild.result = 'FAILURE'
                                base.influxDb()
                                error '[ERROR] 编译失败!'
                            }
                        }
                    }catch(e){
                        println '[ERROR] 编译失败!'
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('package'){
                    if(packageScript){
                        try{
                            dir(packageDir){
                                def status = base.scriptStatus(packageScript)
                                if(status != 0){
                                    currentBuild.result = 'FAILURE'
                                    base.influxDb()
                                    error '[ERROR] 打包失败!'
                                }
                            }
                        }catch(e){
                            println '[ERROR] 打包失败!'
                            println e
                            currentBuild.result = 'FAILURE'
                            base.influxDb()
                            error e.getMessage()
                        }
                    }
                }

                stage('upload'){
                    print "artifact: ${artifactName}"
                    print "deployTarget: ${deployTarget}"
                    try{
                        base.fileUpload(artifactName, deployTarget)
                    }catch(e){
                        println "[ERROR] 上传失败!"
                        println e
                        currentBuild.result = 'FAILURE'
                        base.influxDb()
                        error e.getMessage()
                    }
                }

                stage('retrun artifact'){
                    currentBuild.description = """<span>存储地址:<a href=${storageURL}>${storageURL}</a></span>"""
                    return deployTarget
                }

            }

        }
}